//
//  LLPhotoBrowserDismissAnimation.swift
//  WeiBoShiBing
//
//  Created by Bingo on 15/11/10.
//  Copyright © 2015年 Bingo. All rights reserved.
//

import UIKit

class LLPhotoBrowserDismissAnimation:NSObject,UIViewControllerAnimatedTransitioning{
    
    
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval{
    
        
        return 0.25
    }
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning){
        
        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as!LLPhotoBrowserViewController
        
        let tempImageView = fromVC.dismissTempImageView()
        
        if tempImageView != nil{
        
            transitionContext.containerView()?.addSubview(tempImageView!)
        }
        
        
        let formView = transitionContext.viewForKey(UITransitionContextFromViewKey)
        
        fromVC.collectionView.hidden = true
        
        UIView.animateWithDuration(transitionDuration(nil), animations: { () -> Void in
            
            formView?.alpha = 0
            
            if tempImageView != nil{
            
                tempImageView!.frame = fromVC.dismissTargetFrame()
            
            }
            
            }) { (_) -> Void in
                transitionContext.completeTransition(true)
        }
    }
}
